package com.ruoyi.job.task;

import cn.hutool.system.SystemUtil;
import cn.hutool.system.oshi.OshiUtil;
import co.elastic.clients.elasticsearch.ElasticsearchClient;
import com.ruoyi.common.core.utils.uuid.UUID;
import com.ruoyi.common.core.vo.SystemInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 定时任务-系统监控
 *
 * @author tongshiyang
 */

//   {
//             {
//                   "memorySystemFree": 344, //系统可用内存
//                   "memoryJvmHeapFree": 3038, //jvm可用内存
//                   "memorySystemTotal": 49152, //系统总内存
//                   "memoryJvmNonHeapTotal": 49152, //jvm堆外总内存
//                   "diskTotal": 1907529, //磁盘总空间
//                   "cpuSystemUsage": 11.8, //系统cpu使用率
//                   "diskFree": 1621550, //磁盘可用空间
//                   "clusterNodeId": "jetlinks-platform:8820", //集群节点ID
//                   "memoryJvmHeapTotal": 4001, //jvm总内存
//                   "cpuJvmUsage": 0.1, //jvm cpu使用率
//                   "memoryJvmNonHeapFree": 48964, //jvm堆外可用内存
//                   "id": "eSEeBYEBN57nz4ZBo0WI", // ID
//               },
//               "timeString": "2023-05-16 18:32:27",//时间
//               "timestamp": 1684233147193 //时间
//      }

@Component("monitorTask")
public class MonitorTask {

    @Autowired
    @Qualifier("elasticsearchClient")
    ElasticsearchClient client;
    private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    public void getSystemInfo() throws IOException {
        SystemInfoVo systemInfoVo = new SystemInfoVo();
        systemInfoVo.setId(UUID.fastUUID().toString());

        // cpu
        systemInfoVo.setCpuSystemUsage(OshiUtil.getCpuInfo().getUsed());

        //  jvm总内存 4001,
        systemInfoVo.setMemoryJvmMax(SystemUtil.getRuntimeInfo().getMaxMemory());
        systemInfoVo.setMemoryJvmFree(SystemUtil.getRuntimeInfo().getFreeMemory());
        systemInfoVo.setMemoryJvmTotal(SystemUtil.getRuntimeInfo().getTotalMemory());

        //磁盘
        systemInfoVo.setDiskTotal(OshiUtil.getDiskStores().stream().mapToLong(s->s.getSize()).sum());
        systemInfoVo.setDiskFree(OshiUtil.getDiskStores().stream().mapToLong(s->s.getWriteBytes()).sum());

        //系统内存
        systemInfoVo.setMemorySystemTotal(SystemUtil.getTotalMemory());
        systemInfoVo.setMemorySystemFree(SystemUtil.getFreeMemory());
        //时间
        Date date = new Date();
        systemInfoVo.setTimestamp(date.getTime());
        systemInfoVo.setTimeString(simpleDateFormat.format(date));
        client.index(i-> i.index("system-info")
                .id(systemInfoVo.getId())
                .document(systemInfoVo)
        );
    }

    public static void main(String[] args) {
        System.out.println(SystemUtil.getRuntimeInfo().getMaxMemory());
//        System.out.println(SystemUtil.getHostInfo());
        System.out.println(SystemUtil.getRuntimeInfo().getMaxMemory());
//        System.out.println(SystemUtil.getFreeMemory());
//        System.out.println(SystemUtil.getTotalMemory());
//        System.out.println(SystemUtil.getJavaInfo());
//        System.out.println(OshiUtil.getCpuInfo());
//        System.out.println(OshiUtil.getCpuInfo().getUsed());
//        System.out.println(OshiUtil.getDiskStores().stream().mapToLong(s->s.getSize()).sum());
//        System.out.println(OshiUtil.getSystem());
//        System.out.println(OshiUtil.getHardware());
//        System.out.println(OshiUtil.getNetworkIFs());
//        System.out.println(OshiUtil.getSensors());

        System.out.println(simpleDateFormat.format(new Date()));
    }
}
